package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.presenter.MarketPresenterImpl;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/12/20
 */

public class MarketModelImpl implements MarketModel, VolleyJsonObjectCallBack {
    MarketPresenterImpl marketPresenter;

    @Override
    public void sendUrl(String url, MarketPresenterImpl marketPresenter) {
        this.marketPresenter = marketPresenter;
        volleyUtil.setJsonObjectRequest(url, this);
    }

    @Override
    public void sendSetUrl(String url, MarketPresenterImpl marketPresenter) {
        this.marketPresenter = marketPresenter;
        volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res = response.getInt("res");
                    marketPresenter.recRes(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                marketPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
//            RestaurantInfo restaurantInfo= JsonUtil.getObjectFromString(response.getString("res"),RestaurantInfo.class);
            List<RestaurantInfo> restaurantInfos = JsonUtil.getListFromString(response.getString("res"), RestaurantInfo.class);
            if (restaurantInfos.size() != 0) {
                marketPresenter.recResInfo(restaurantInfos.get(0));
            } else {
                marketPresenter.recResInfo(null);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        marketPresenter.sendError(error);
    }
}